package org.dromara.system.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.TroubleBo;
import org.dromara.system.domain.vo.TroubleVo;
import org.dromara.system.domain.Trouble;
import org.dromara.system.mapper.TroubleMapper;
import org.dromara.system.service.ITroubleService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 隐患Service业务层处理
 *
 * @author Lion Li
 * @date 2025-09-15
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class TroubleServiceImpl implements ITroubleService {

    private final TroubleMapper baseMapper;

    /**
     * 查询隐患
     *
     * @param id 主键
     * @return 隐患
     */
    @Override
    public TroubleVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询隐患列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 隐患分页列表
     */
    @Override
    public TableDataInfo<TroubleVo> queryPageList(TroubleBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Trouble> lqw = buildQueryWrapper(bo);
        Page<TroubleVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的隐患列表
     *
     * @param bo 查询条件
     * @return 隐患列表
     */
    @Override
    public List<TroubleVo> queryList(TroubleBo bo) {
        LambdaQueryWrapper<Trouble> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Trouble> buildQueryWrapper(TroubleBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Trouble> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(Trouble::getId);
        lqw.like(StringUtils.isNotBlank(bo.getDangerName()), Trouble::getDangerName, bo.getDangerName());
        lqw.eq(StringUtils.isNotBlank(bo.getHazardCode()), Trouble::getHazardCode, bo.getHazardCode());
        lqw.eq(bo.getRiskMeasureId() != null, Trouble::getRiskMeasureId, bo.getRiskMeasureId());
        lqw.eq(bo.getRecordId() != null, Trouble::getRecordId, bo.getRecordId());
        lqw.eq(bo.getDangerLevel() != null, Trouble::getDangerLevel, bo.getDangerLevel());
        lqw.eq(bo.getRegistTime() != null, Trouble::getRegistTime, bo.getRegistTime());
        lqw.eq(StringUtils.isNotBlank(bo.getRegistrant()), Trouble::getRegistrant, bo.getRegistrant());
        lqw.eq(bo.getDangerSrc() != null, Trouble::getDangerSrc, bo.getDangerSrc());
        lqw.eq(StringUtils.isNotBlank(bo.getEnforcementId()), Trouble::getEnforcementId, bo.getEnforcementId());
        lqw.eq(bo.getDangerManageType() != null, Trouble::getDangerManageType, bo.getDangerManageType());
        lqw.eq(bo.getHazardDangerType() != null, Trouble::getHazardDangerType, bo.getHazardDangerType());
        lqw.eq(bo.getHazardCategory() != null, Trouble::getHazardCategory, bo.getHazardCategory());
        lqw.eq(StringUtils.isNotBlank(bo.getDangerDesc()), Trouble::getDangerDesc, bo.getDangerDesc());
        lqw.eq(StringUtils.isNotBlank(bo.getDangerReason()), Trouble::getDangerReason, bo.getDangerReason());
        lqw.eq(StringUtils.isNotBlank(bo.getControlMeasures()), Trouble::getControlMeasures, bo.getControlMeasures());
        lqw.eq(StringUtils.isNotBlank(bo.getCost()), Trouble::getCost, bo.getCost());
        lqw.eq(StringUtils.isNotBlank(bo.getLiablePerson()), Trouble::getLiablePerson, bo.getLiablePerson());
        lqw.eq(bo.getDangerManageDeadline() != null, Trouble::getDangerManageDeadline, bo.getDangerManageDeadline());
        lqw.eq(StringUtils.isNotBlank(bo.getCheckAcceptPerson()), Trouble::getCheckAcceptPerson, bo.getCheckAcceptPerson());
        lqw.eq(bo.getCheckAcceptTime() != null, Trouble::getCheckAcceptTime, bo.getCheckAcceptTime());
        lqw.eq(StringUtils.isNotBlank(bo.getCheckAcceptComment()), Trouble::getCheckAcceptComment, bo.getCheckAcceptComment());
        lqw.eq(bo.getState() != null, Trouble::getState, bo.getState());
        lqw.eq(StringUtils.isNotBlank(bo.getAttachments1()), Trouble::getAttachments1, bo.getAttachments1());
        lqw.eq(StringUtils.isNotBlank(bo.getAttachments2()), Trouble::getAttachments2, bo.getAttachments2());
        return lqw;
    }

    /**
     * 新增隐患
     *
     * @param bo 隐患
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(TroubleBo bo) {
        Trouble add = MapstructUtils.convert(bo, Trouble.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改隐患
     *
     * @param bo 隐患
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(TroubleBo bo) {
        Trouble update = MapstructUtils.convert(bo, Trouble.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Trouble entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除隐患信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
